package com.example.zp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class LocalMusicAdapter extends RecyclerView.Adapter<LocalMusicAdapter.LocalMusicViewHolder> {

    Context context;
    List<LocalMusicBean> musics;

    OnItemClickListener onItemClickListener;

    // 接口回调
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    // 开放一个接口，用于传递点击事件
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public LocalMusicAdapter(Context context, List<LocalMusicBean> musics) {
        this.context = context;
        this.musics = musics;
    }

    @NonNull
    @Override
    public LocalMusicViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_music, parent, false);
        LocalMusicViewHolder holder = new LocalMusicViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull LocalMusicViewHolder holder, final int position) {
        LocalMusicBean musicBean = musics.get(position);
        holder.idTv.setText(musicBean.getId());
        holder.songTv.setText(musicBean.getSong());
        holder.singerTv.setText(musicBean.getSinger());
        holder.durationTv.setText(musicBean.getDuration());

        holder.itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return musics.size();
    }

    class LocalMusicViewHolder extends RecyclerView.ViewHolder {
        TextView idTv, songTv, singerTv, durationTv;
        public LocalMusicViewHolder(@NonNull View itemView) {
            super(itemView);
            idTv = itemView.findViewById(R.id.item_music_num);
            songTv = itemView.findViewById(R.id.item_music_song);
            singerTv = itemView.findViewById(R.id.item_music_singer);
            durationTv = itemView.findViewById(R.id.item_music_duration);
        }
    }
}
